package com.dianping.tpfun.supervised.decisiontree.bean;

import com.google.common.collect.Lists;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author taiwei.zhang
 */
public class DefaultDecisionTreeNode {

    DefaultDecisionTreeNode parent;
    List<DefaultDecisionTreeNode> childList;
    LinkedHashMap<Comparable , DefaultDecisionTreeNode> partionChildMap;
    List<Comparable> partionPoints;
    //该节点信息增益比
    Double informationGainRatio;
    DefaultFeature feature;
    boolean isLeaf;
    DefaultLabel label;

    public DefaultDecisionTreeNode determinNextChildNode(DefaultData data) throws Exception{

        DefaultDecisionTreeNode result = null;
        if(isLeaf){
            throw new Exception("already get a leaf!");
        }

        Comparable dataFeatureValue = data.getDataDims()[feature.getIndex()].getData();
        if (feature.getType() == FeatureType.Discrete.getValue()) {
           for(Comparable point : partionChildMap.keySet()){
               if(point.equals(dataFeatureValue)){
                   result = partionChildMap.get(point);
               }

           }
        } else if (feature.getType() == FeatureType.Continuity.getValue()) {
            for(Comparable point : partionChildMap.keySet()){
                while(point.compareTo(dataFeatureValue)<0){
                    continue;
                }
                if(point.compareTo(dataFeatureValue)>=0){
                    result = partionChildMap.get(point);
                }
            }
        }
        return result;
    }

    public List<DefaultDecisionTreeNode> getChildList() {
        return childList;
    }

    public void setChildList(List<DefaultDecisionTreeNode> childList) {
        this.childList = childList;
    }

    public DefaultFeature getFeature() {
        return feature;
    }

    public void setFeature(DefaultFeature feature) {
        this.feature = feature;
    }

    public Double getInformationGainRatio() {
        return informationGainRatio;
    }

    public void setInformationGainRatio(Double informationGainRatio) {
        this.informationGainRatio = informationGainRatio;
    }

    public boolean isLeaf() {
        return isLeaf;
    }

    public void setIsLeaf(boolean isLeaf) {
        this.isLeaf = isLeaf;
    }

    public DefaultDecisionTreeNode getParent() {
        return parent;
    }

    public void setParent(DefaultDecisionTreeNode parent) {
        this.parent = parent;
    }

    public List<Comparable> getPartionPoints() {
        return partionPoints;
    }

    public void setPartionPoints(List<Comparable> partionPoints) {
        this.partionPoints = partionPoints;
    }

    public LinkedHashMap<Comparable, DefaultDecisionTreeNode> getPartionChildMap() {
        return partionChildMap;
    }

    public void setPartionChildMap(LinkedHashMap<Comparable, DefaultDecisionTreeNode> partionChildMap) {
        this.partionChildMap = partionChildMap;
    }

    public DefaultLabel getLable() {
        return label;
    }

    public void setLabel(DefaultLabel lable) {
        this.label = lable;
    }

}
